package com.example.demo.controller;


import com.example.demo.model.entity.UserEntity;
import com.example.demo.model.entity.WeatherEntity;
import com.example.demo.model.vo.ResponseVo;
import com.example.demo.service.WeatherService;
import com.example.demo.untils.BuildResponseUtils;
import com.example.demo.untils.SendMailUntil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "天气信息控制器")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;


    @PostMapping("/getCityWeather")
    public ResponseVo<?> getCityWeather(@RequestBody WeatherEntity weatherEntity){
        try{
            List<Map<String,String>> we = weatherService.getCityWeatherDataList(weatherEntity.getCityCode());
            return BuildResponseUtils.buildResponse(we);
        }catch (Exception e){
            e.printStackTrace();
            return BuildResponseUtils.error();
        }
    }



    @PostMapping("/subscribeWeather")
    public ResponseVo<?> subscribeWeather(@RequestParam("selectedCity")String selectCity, @RequestParam("date")String date, HttpServletRequest request){
        System.out.println(selectCity + "/" + date);
        HttpSession session = request.getSession();
        UserEntity userEntity = (UserEntity) session.getAttribute("user");
        String email = userEntity.getEmail();
        try {
            SendMailUntil.sendEmail(email,"天气预报信息预约","你已经成功订阅"+selectCity+"的天气信息，订阅时间为"+date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BuildResponseUtils.success();
    }
}
